//
//  OderStateViewController.m
//  Feast
//
//  Created by 胡光健 on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "OderStateViewController.h"
#import "OderStateCell.h"
#import "FtOderDetailViewController.h"
#import "FtTreatOderCell.h"

@interface OderStateViewController ()

@end

@implementation OderStateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.navTitle;
}
-(void)configTableView {
    if (self.type == treatOderState) {
         self.tableViewStyle = UITableViewStyleGrouped;
    }else {
        self.tableViewStyle = UITableViewStylePlain;
    }
    [super configTableView];
    if (self.type == treatOderState) {
        UIView *view = [self headSection];
        [self.view addSubview:view];
        [self.view bringSubviewToFront:view];
        self.tableView.frame = CGRectMake(0, 70, mScreenWidth, mScreenHeight - 70 -64);
        [self.tableView registerCellByClassName:@"FtTreatOderCell"];
    }
    [self.tableView registerCellByNibName:@"OderStateCell"];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.type == treatOderState) {
        return 4;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type == treatOderState) {
        return 3;
    }
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OderStateCell * cell =[tableView dequeueReusableCellWithIdentifier:@"OderStateCell"];
    FtTreatOderCell*treatOderCell = [tableView dequeueReusableCellWithIdentifier:@"FtTreatOderCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    treatOderCell.backgroundColor = [UIColor clearColor];
    if (self.type == treatOderState) {/**待接单*/

        return cell;
    }else {
        if (self.type == treatDeliveryState) {//待配送
            cell.dealRefundBtn.hidden = YES;
            cell.refundView.hidden = YES;
            cell.deliveryLabel.hidden = YES;
            [cell.okDeliveryOrOderBtn setTitle:@"确认送达" forState:UIControlStateNormal];
        }else if (self.type == finishState) {//已完成
            cell.dealRefundBtn.hidden = YES;
            cell.refundView.hidden = YES;
            cell.okDeliveryOrOderBtn.hidden = YES;
        }else if (self.type == refundState) {//退款
            cell.okDeliveryOrOderBtn.hidden = YES;
            cell.deliveryLabel.hidden = YES;
        }
//        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        return cell;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.type == treatOderState) {
        return [self configCellHeader];
    }
    return [self headSection];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (self.type == treatOderState) {
        return [self configCellFooter];
    }
    UIView *view = [UIView new];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.type == treatOderState) {
        return 30;
    }
    return 70;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.type == treatOderState) {
        return 55;
    }
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == treatOderState) {/**待接单*/
        return 121;
    }else {
         return 121.5;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FtOderDetailViewController * vc = [[FtOderDetailViewController alloc]init];
    if (self.type == treatOderState) {
        vc.navTitle = @"";
    }else if (self.type == treatDeliveryState) {
        vc.navTitle = @"";
    }else if (self.type == finishState) {
        vc.navTitle = @"订单已完成";
    }else if (self.type == refundState) {
        vc.navTitle = @"申请退款";
    }
    [self.navigationController pushViewController:vc animated:YES];
}

-(UIView *)headSection {
    UIView * view =[UIView new];
    view.frame =CGRectMake(0, 0, mScreenWidth, 70);
    view.backgroundColor = HJColorRGB(243);
    
    UIView * labeView = [[UIView alloc]initWithFrame:CGRectMake(12, 10, view.width - 24, 50)];
    labeView.backgroundColor = [UIColor whiteColor];
    [view addSubview:labeView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12, labeView.height * 0.5- 10, 80, 20)];
    label.text = @"选择时间";
    label.font = [UIFont systemFontOfSize:mScreenWidth / 414 * 16];
    label.textColor = HJColorRGB(96);
    label.textAlignment = NSTextAlignmentLeft;
    [labeView addSubview:label];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(labeView.width -12 -10, labeView.height * 0.5- 7, 9, 15)];
    imageView.image = HJImage(@"Combined Shape Copy 3");
    [labeView addSubview:imageView];
    
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(labeView.width - 22-10 -200,  labeView.height * 0.5- 10, 200, 20)];
    timeLabel.text = @"12：00-14：30";
    timeLabel.font = [UIFont systemFontOfSize:mScreenWidth / 414 * 16];
    timeLabel.textColor = HJColorRGB(155);
    timeLabel.textAlignment = NSTextAlignmentRight;
    [labeView addSubview:timeLabel];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, labeView.width, labeView.height);
    [btn addTarget:self action:@selector(selectedTimeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [labeView addSubview:btn];
    [labeView bringSubviewToFront:btn];
    
    return view;
}

-(void)selectedTimeBtnClicked {
    
}

-(UIView *)configCellHeader {
    UIView * labeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, 30)];
    labeView.backgroundColor = HJColorRGB(243);
    
    UIView *headView = [[UIView alloc]init];
    headView.frame = CGRectMake(12, 0, labeView.width-24, 30);
    headView.backgroundColor = [UIColor whiteColor];
    [labeView addSubview:headView];
    
    UILabel * leftLabel = [self configWithLabel:@"订单号：2536743" textColor:HJColorRGB(124) textAlignment:NSTextAlignmentLeft frame:CGRectMake(11, headView.height *0.5-9, 120, 18) font:HJFontSize(mScreenWidth / 414 *14)];
    [headView addSubview:leftLabel];
    
    UILabel * rightLabel = [self configWithLabel:@"待接单" textColor:HJColor(255, 150, 0) textAlignment:NSTextAlignmentRight frame:CGRectMake(headView.width - 60-12, headView.height*0.5-9, 60, 18) font:HJFontSize(mScreenWidth / 414 *14)];
    [headView addSubview:rightLabel];
    
    return labeView;
}

-(UIView *)configCellFooter {
    UIView * footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, 55)];
    footer.backgroundColor =HJColorRGB(243);
    
    UIView *footerView = [UIView new];
    footerView.frame = CGRectMake(12, 0, footer.width-24, 55);
    footerView.backgroundColor= [UIColor whiteColor];
    [footer addSubview:footerView];
    
    UILabel * label = [self configWithLabel:@"2017.08.19 12:20" textColor:HJColorRGB(124) textAlignment:NSTextAlignmentLeft frame:CGRectMake(11, footerView.height *0.5-9-10, 120, 18) font:HJFontSize(mScreenWidth / 414 *14)];
    [footerView addSubview:label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(footerView.width - 90 -12, footerView.height *0.5-13-10, 90, 26);
    [btn setTitle:@"确认接单" forState:UIControlStateNormal];
    btn.titleLabel.font = HJFontSize(mScreenWidth/414 *14);
    [btn setTitleColor:HJColor(255, 150, 0) forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = HJColor(255, 150, 0).CGColor;
    btn.layer.cornerRadius = 3;
        [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:btn];
    
    UIView * view = [UIView new];
    view.frame = CGRectMake(0, footerView.height-10, footerView.width, 10);
    view.backgroundColor = HJColorRGB(243);
    [footerView addSubview:view];
    return footer;
}

-(void)btnClicked {
    
}

@end
